library(survey)
library(dplyr)
library(ggplot2)
library(gridExtra)

# Figure 9.4
summary(factor(df$Q5A))
summary(factor(df$Q6A))
df2 <- df[df$Q11<5,]

df2$Q11T <- NA
df2$Q11T[df2$Q11>2] <- 0
df2$Q11T[df2$Q11<3] <- 1

# Important
df2$Q5A[df2$Q5A>3] <- NA
df2$Q5A[df2$Q5A==3] <- 0
df2$Q5A[df2$Q5A==2] <- 0.5
df2$Q5A[df2$Q5A==1] <- 1

df2$Q5B[df2$Q5B>3] <- NA
df2$Q5B[df2$Q5B==3] <- 0
df2$Q5B[df2$Q5B==2] <- 0.5
df2$Q5B[df2$Q5B==1] <- 1

df2$Q5C[df2$Q5C>3] <- NA
df2$Q5C[df2$Q5C==3] <- 0
df2$Q5C[df2$Q5C==2] <- 0.5
df2$Q5C[df2$Q5C==1] <- 1

df2$Q5D[df2$Q5D>3] <- NA
df2$Q5D[df2$Q5D==3] <- 0
df2$Q5D[df2$Q5D==2] <- 0.5
df2$Q5D[df2$Q5D==1] <- 1

df2$Q5E[df2$Q5E>3] <- NA
df2$Q5E[df2$Q5E==3] <- 0
df2$Q5E[df2$Q5E==2] <- 0.5
df2$Q5E[df2$Q5E==1] <- 1

df2$Q5F[df2$Q5F>3] <- NA
df2$Q5F[df2$Q5F==3] <- 0
df2$Q5F[df2$Q5F==2] <- 0.5
df2$Q5F[df2$Q5F==1] <- 1

df2$Q5G[df2$Q5G>3] <- NA
df2$Q5G[df2$Q5G==3] <- 0
df2$Q5G[df2$Q5G==2] <- 0.5
df2$Q5G[df2$Q5G==1] <- 1

df2$Q5H[df2$Q5H>3] <- NA
df2$Q5H[df2$Q5H==3] <- 0
df2$Q5H[df2$Q5H==2] <- 0.5
df2$Q5H[df2$Q5H==1] <- 1

colnames(df2) # 25:32

df2$REAL <- rowSums(df2[,c(25,32)],na.rm=FALSE)/2 # Q5A+Q5H
df2$HUMAN <- rowSums(df2[,c(28,30)],na.rm=FALSE)/2 # Q5D+Q5F
df2$SECURITY <- rowSums(df2[,c(29,31)],na.rm=FALSE)/2 # Q5E+Q5G
df2$DOMESTIC <- rowSums(df2[,c(26,27)],na.rm=FALSE)/2 # Q5B+Q5C

df2 <- df2[df2$Q11 < 5 & df2$P_ASSIGN1==1,]

w2_design <-
  svydesign(
    id = ~ 1,
    weights = ~ weight2,
    data = df2
  )

f.real <- svyby(~ REAL, ~Q11T+party, w2_design, svymean, na.rm = TRUE)
f.hum <- svyby(~ HUMAN, ~Q11T+party, w2_design, svymean, na.rm = TRUE)
f.sec <- svyby(~ SECURITY, ~Q11T+party, w2_design, svymean, na.rm = TRUE)
f.dom <- svyby(~ DOMESTIC, ~Q11T+party, w2_design, svymean, na.rm = TRUE)

# Realpolitik
g.real <- ggplot(f.real, aes(x=factor(party),y=REAL,fill=factor(Q11T),label=round(REAL,digits=2)))+
  geom_col(position = position_dodge(),width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  theme(legend.position = "null")+
  scale_x_discrete(labels=c("Democrat",'Independent',"Republican"))+
  xlab("")+
  ylab("Importance of Realpolitik Missions\n")

# Humanitarian
g.hum <- ggplot(f.hum, aes(x=factor(party),y=HUMAN,fill=factor(Q11T),label=round(HUMAN,digits=2)))+
  geom_col(position = position_dodge(),width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  theme(legend.position = "null")+
  scale_x_discrete(labels=c("Democrat",'Independent',"Republican"))+
  xlab("")+
  ylab("Importance of Humanitarian Missions\n")

# Homeland
g.sec <- ggplot(f.sec, aes(x=factor(party),y=SECURITY,fill=factor(Q11T),label=round(SECURITY,digits=2)))+
  geom_col(position = position_dodge(),width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+ 
  theme(legend.position = "null")+
  scale_x_discrete(labels=c("Democrat",'Independent',"Republican"))+
  xlab("")+
  ylab("Importance of Homeland Security\n")

# Domestic
g.dom <- ggplot(f.dom, aes(x=factor(party),y=DOMESTIC,fill=factor(Q11T),label=round(DOMESTIC,digits=2)))+
  geom_col(position = position_dodge(),width=0.5)+
  geom_text(size=3,position=position_dodge(1),vjust=-.5)+
  ylim(0,1)+
  theme(legend.position = 'null')+
  scale_x_discrete(labels=c("Democrat",'Independent',"Republican"))+
  xlab("")+
  ylab("Importance of Domestic Social Work\n")

grid.arrange(g.real,g.hum,g.sec,g.dom,ncol=2,nrow=2)
